package poli.tidia.mobile;

import java.util.List;

import poli.tidia.mobile.model.Site;
import poli.tidia.mobile.model.AnnouncementMenuItem;
import poli.tidia.mobile.proxy.AnnouncementsProxy;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AnnouncementListActivity extends ListActivity {
	@Override
	public void onCreate (Bundle bundle){
		super.onCreate(bundle);
		Bundle b = getIntent().getExtras();
    	if (b != null) {
			Site selectedSite = (Site) b.getSerializable("selectedSite");
			AnnouncementsProxy annProxy = new AnnouncementsProxy();
			List<AnnouncementMenuItem> announcements = annProxy.getLastAnnouncements(selectedSite);
			setListAdapter(new ArrayAdapter<AnnouncementMenuItem>(this, R.layout.sites_menu, announcements));
			
			ListView lv = getListView();
			lv.setBackgroundColor(Color.WHITE);
			lv.setTextFilterEnabled(true);
	
			lv.setOnItemClickListener(new OnItemClickListener() {
			    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			    	String selectedAnnouncementUrl = ((AnnouncementMenuItem)parent.getItemAtPosition(position)).getUrl();
			    	Intent selectedSiteIntent = new Intent(AnnouncementListActivity.this, AnnouncementDetailsActivity.class);
			    	selectedSiteIntent.putExtra("selectedAnnouncement", selectedAnnouncementUrl);
			    	startActivity(selectedSiteIntent);
			    }
			  });
    	}
	}	
}
